//index.js
//获取应用实例
var app = getApp()
let garageData = require('garageData')
let QQMapWX = require('../../utils/qqmap-wx-jssdk')
let qqmapsdk = new QQMapWX({
  key: 'GIEBZ-WHU3X-WN74E-ZIWQE-FFUC3-NBF4B' // 必填
});
let mapkey = 'GIEBZ-WHU3X-WN74E-ZIWQE-FFUC3-NBF4B'

Page({
  data: {
    latitude: '',
    longitude: '',
    scale: 15,   //缩放级别
    routeFlag: false,//实况
    address: '',
    markers: [
      {
        id: 1,
        name: '武汉大学停车场',
        longitude: 114.360734,
        latitude: 30.541093,
        iconPath: "/assets/images/garage/Marker1_Activated@3x.png",
        width: 40,
        address: '',
        height: 45,
        callout: {
          content: '武汉大学智象智能停车场',
          bgColor: '#fff',
          padding: '7rpx',
          fontSize: 13,
          borderRadius: '1px',
          borderWidth: '1px',
          borderColor: '#1296db',
          display: 'ALWAYS'
        }
      }, {
        id: 2,
        name: '武汉理工大学科技园停车场',
        longitude: 114.3972679762268,
        latitude: 30.44470581245983,
        iconPath: "/assets/images/garage/Marker1_Activated@3x.png",
        width: 40,
        address: '',
        height: 45,
        callout: {
          content: '武汉理工大学科技园智象智能停车场',
          bgColor: '#fff',
          padding: '7rpx',
          fontSize: 13,
          borderRadius: '1px',
          borderWidth: '1px',
          borderColor: '#1296db',
          display: 'ALWAYS'
        }
      }
    ]
  },
  bindregionchange: function (e) {
    if (e.type == "begin") {
      // console.log("begin");
    } else if (e.type == "end") {
      // console.log("end");
    }
  },
  bindcontroltap: function (e) {
    // 点击事件选项
    switch (e.controlId) {
      // 当点击图标location.png的图标，则触发事件movetoPositioon()
      case 1: this.movetoPosition();
        break;
      // 当点击图标use.png的图标，则触发事件scanCode.png()
      case 2: wx.scanCode({
        success: (res) => {
          console.log(res);
        }
      })
        break;
      case 3: wx.navigateTo({
        url: '../logs/logs',
        success: (res) => {
          console.log(res);
        }
      })
        break;
      case 5: this.setRouFlag()
        this.onLoad()
        break;

    }
  },
  //移动到当前位置
  movetoPosition: function () {
    this.mapCtx.moveToLocation();
  },

  //事件处理函数
  onLoad: function () {
    // console.log('onLoad')
    //调用应用实例的方法获取全局数据
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        console.log('res', res)
        this.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        })
      }
    }),
    wx.getSystemInfo({

      success: (res) => {
        console.log(res)
        this.setData({
          controls: [{
            id: 1,
            iconPath: '/assets/images/garage/location.png',
            position: {
              left: 20,
              top: res.windowHeight - 80,
              width: 50,
              height: 50
            },
            clickable: true
          },

          {
            id: 2,
            iconPath: '/assets/images/garage/use.png',
            position: {
              left: res.windowWidth / 2 - 45,
              top: res.windowHeight - 100,
              width: 90,
              height: 90
            },
            clickable: true
          },
          {
            id: 3,
            iconPath: '/assets/images/garage/warn.png',
            position: {
              left: res.windowWidth - 70,
              top: res.windowHeight - 80,
              width: 50,
              height: 50
            },
            clickable: true
          },
          {
            id: 4,
            iconPath: '/assets/images/garage/marker.png',
            position: {
              left: res.windowWidth / 2 - 11,
              top: res.windowHeight / 2 - 45,
              width: 28,
              height: 45
            },
            clickable: true
          },
          {
            id: 5,
            iconPath: '/assets/images/garage/shikuang' + this.data.routeFlag + '.png',
            position: {
              left: res.windowWidth - 50,
              top: res.windowHeight - 500,
              width: 40,
              height: 40
            },
            clickable: true
          }
          ]
        })
      }
    }),
    this.setMarker()
  },
  onShow: function () {
    // console.log("onShow");
    this.mapCtx = wx.createMapContext("zxMap");
    this.movetoPosition();
  },
  //获取车库标识
  getGarageMarkers() {
    let markers = new Array()
    for (let e in garageData) {
      let marker = this.createMarker(e);
      markers.push(marker);
    }
    return markers;
  },

  //创建地图上的车库图标
  createMarker(point) {
    let marker = {
      iconPath: "/assets/images/garage/车库位置满.png",
      id: point.id || 0,
      name: point.name || '',
      latitude: point.latitude,
      longitude: point.longitude,
      width: 25,
      height: 48
    };
    return marker;
  },
  //设置路径规划的点
  setMarker(e) {
    //e.deatail.markerId
    // console.log(e)
    let that = this
    let k = e.detail.markerId
    k = k - 1
    // this.routePlan(k)
    let marker = that.data.markers[k]

    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + marker.latitude + ',' + marker.longitude + '&key=' + mapkey,
      success(res) {
        marker.address = res.data.result.address
        that.setData({
          address: res.data.result.address
        })
      }
    })

    wx.openLocation({
      latitude: marker.latitude,
      longitude: marker.longitude,
      scale: 8, // 缩放比例
      name: marker.name,
      address: marker.address,
      success(res) {
        console.log(res)
      },
      fail(res) {
        console.log(res)
      }
    })
  },

  getAddress(marker) {
    let that = this
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + marker.latitude + ',' + marker.longitude + '&key=' + mapkey,
      success(res) {
        marker.address = res.data.result.address
        that.setData({
          address: res.data.result.address
        })
      }
    })
  },
  //路径规划
  routePlan(k) {
    let that = this
    let plugin = requirePlugin('routePlan')
    let key = "GIEBZ-WHU3X-WN74E-ZIWQE-FFUC3-NBF4B"
    let referer = '智象停车'
    let marker = that.data.markers[k]
    let endPoint = JSON.stringify({
      'name': marker.name,
      'latitude': marker.latitude,
      'longitude': marker.longitude
    })
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint,
    })
  },
  //路况设置
  setRouFlag() {
    let that = this
    that.setData({
      routeFlag: !(that.data.routeFlag)
    })
  },

  //计算一点到多点的距离


})
